//index.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js')

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    localPort: 0,
    wifiName: 'Unknown',
    remoteIp: '',
    remotePort: 0,
    message: '',
    messageList: [],
    udp: null,
  },


  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },


  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    this.initListener()
    this.initUdp()
  },


  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },


  initListener: function() {
    var obj = this
    wx.onWifiConnected(function(res){
      obj.setData ({
        wifiName: res.wifi['SSID']
      })
    }) 
    wx.startWifi({
      success: function(res) {

      },
      fail: function(res) {
        util.logD(res)
      },
      complete: function(res) {

      },
    }) 
  },


  initUdp: function() {
    var udp = wx.createUDPSocket()
    if (udp == null) {
      return
    }
    var port = udp.bind()
    this.setData ({
      localPort: port,
      udp: udp
    })

    this.data.udp.onListening(function (res) {
      util.logD('监听中...')
      util.logD(res)
    })

    var that = this
    this.data.udp.onMessage(function (res) {
      util.logD(res)
      var str = util.ab2str(res.message)
      var list = that.data.messageList
      var obj = {
        text: str,
        from: 'remote'
      }
      list.push(obj)
      // 同步页面数据
      that.setData ({
        messageList: list
      })
    })
  },


  addIp: function (event) {
    this.setData({
      remoteIp: event.detail.value
    })
  },


  addPort: function (event) {
    this.setData({
      remotePort: event.detail.value
    })
  },


  addMessage: function(event) {
    util.logD(event)
    this.data.message = event.detail.value
  },


  sendMessage: function(e) {
    var msg = this.data.message
    if (msg.trim() == '') {
      return
    }
    if (this.data.udp == null) {
      return
    }
    if (this.data.remoteIp == '') {
      return
    }
    var remoteAddr = this.data.remoteIp
    var remotePort = this.data.remotePort
    this.data.udp.send({
      address: remoteAddr,
      port: remotePort,
      message: msg
    })

    var list = this.data.messageList
    var obj = {
      text: msg,
      from: 'local'
    }
    list.push(obj)
    // 同步页面数据
    this.setData ({
      messageList: list
    })
  },
})
